import requests
import pandas as pd


headers = {
    'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36'
}
url = 'https://movie.douban.com/j/chart/top_list'
params = {
    "type": "24",
    "interval_id": "100:90",
    "action": "",
    "start": "20",
    "limit": "20"
}
movie_data = requests.get(url=url, headers=headers, params=params).json()
# 电影名字，评分，导演，上映时间
table = pd.DataFrame(columns=['title','score','actors','year'])
index = 0 #初始的行索引
for dic in movie_data:
    title = dic['title']
    score = dic['score']
    actors = dic['actors']
    # 用于将数据分割存进数组
    actors = ','.join(actors)
    year = dic['release_date']

    table.loc[index] = [title,score,actors,year]

    index += 1
    print(title,':爬取保存成功！')

table.to_excel('movie_data.xlsx',sheet_name='movie')